package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.http.okhttp.HttpTask;
import com.lib.util.ResUtil;
import com.lib.util.ToastUtil;
import com.qbw.actionmanager.ActionManager;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.action.AddVehicleAction;
import com.qbw.app.databinding.VehicleActivityBindBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.main.activity.MainActivity;
import com.qbw.app.module.user.activity.UserLoginRegistActivity;
import com.qbw.app.request.impl.VehicleRequest;

import cn.jpush.android.api.JPushInterface;

/**
 * 添加车辆诺马
 * @author xiansx
 * @date 2020-08-13
 */
public class VehicleBindActivity extends CommonActivity<VehicleActivityBindBinding> {

    public static final int TYPE_FROM_DEFAULT = 0;
    /**
     * 注册或者登录之后，没有车辆的情况下，会跳转到此页面
     */
    public static final int TYPE_FROM_NO_VEHICLE = 1;

    private int mType;

    public static void startActivity(Context context) {
        startActivity(context, TYPE_FROM_DEFAULT);
    }

    public static void startActivity(Context context, int type) {
        Intent intent = new Intent(context, VehicleBindActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(Constant.KEY_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_bind;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mType = args.getInt(Constant.KEY_TYPE);
        mBinding.title.setListener(this);
        mBinding.tvAdd.setOnClickListener(this);
        mBinding.tvScan.setOnClickListener(this);
        mBinding.executePendingBindings();
        if (mType == TYPE_FROM_NO_VEHICLE) {
            UniversalToastUtil.showToast(ResUtil.getString(R.string.please_add_vehicle), 3000);
        }
    }

    /**
     * 添加车辆
     */
    private void addVehicleNm() {
        if (!checkInput()) {
            return;
        }
        String deviceCode = mBinding.etDevicecode.getText().toString();
        String devicePassword = mBinding.etDevicecodePwd.getText().toString();
        if(!TextUtils.isEmpty(deviceCode) &&
                !TextUtils.isEmpty(devicePassword))
        mGeneralManager.getRequestManager()
                .getVehicleRequest()
                .addVehicleNm(deviceCode, devicePassword,
                        this)
                .execute();
    }

    @Override
    public void onLeftAreaClick() {
        if (mType == TYPE_FROM_NO_VEHICLE) {
            // 注销账号
            mGeneralManager.getRequestManager()
                    .getCommonRequest()
                    .stopPush(JPushInterface.getRegistrationID(this), null)
                    .execute();
            mGeneralManager.getUserManager().logout();

            UserLoginRegistActivity.start(this);
        }
        super.onLeftAreaClick();
    }

    @Override
    public void onBackPressed() {
        onLeftAreaClick();
    }

    private boolean checkInput() {
        String deviceCode = mBinding.etDevicecode.getText().toString();
        String devicePassword = mBinding.etDevicecodePwd.getText().toString();
        if (TextUtils.isEmpty(deviceCode)) {
            UniversalToastUtil.showToast(R.string.nm_vehicle_add_devicecode_hint);
            return false;
        } else if (TextUtils.isEmpty(devicePassword)) {
            UniversalToastUtil.showToast(R.string.nm_vehicle_add_devicepwd_hint);
            return false;
        }
        return true;
    }

    @Override
    public void onHttpStart(HttpTask httpTask) {

    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_ADD_V)) {
            String deviceCode = mBinding.etDevicecode.getText().toString();
            String devicePassword = mBinding.etDevicecodePwd.getText().toString();
            Vehicle vehicle = new Vehicle();
            vehicle.setDeviceCode(deviceCode);
            vehicle.setDevicePassword(devicePassword);
            ActionManager.getInstance()
                    .triggerAction(new AddVehicleAction(vehicle));
            closeLoadingDialog();
            ToastUtil.showToast(this,R.string.vehicle_successfully_added, Toast.LENGTH_LONG);
//            if (mType == TYPE_FROM_NO_VEHICLE) {
//                MainActivity.start(this);
//            }
            MainActivity.start(this);
            finish();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        UniversalToastUtil.showToast(HttpRequestUtil.getMessage(errorCode, message));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(Constant.KEY_TYPE, mType);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_add) {
            addVehicleNm();
        }else if (v.getId() == R.id.tv_scan) {
            VehicleBindByScanActivity.startActivity(this);
            this.finish();
        }
    }

}
